/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rnavizexperimentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Elias la Cour
 */
public class View extends JPanel {

    private static int CANVAS_SIZE = 512;
    private Structure structure = null;
    final static BasicStroke stroke = new BasicStroke(1.0f);
    Graphics2D g2d;
    
    View(){
    }
    View(Structure structure){
        this.structure=structure;
    }

    public void createLayout() {
        JFrame topFrame = new JFrame();
        topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //topFrame.pack();
        topFrame.setSize(CANVAS_SIZE, CANVAS_SIZE);
        topFrame.setTitle("RNA Visualazzzzers PEW PEW!!!");
        //topFrame.add(new MyCircle(new InputHandler().smallInput()));
        topFrame.setVisible(true);
        topFrame.add(new View());
    }

    @Override
    protected void paintComponent(Graphics g) {
        g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.BLUE);
        g2d.setStroke(stroke);
        super.paintComponent(g2d);
        int xMiddle;
        int yMiddle;
        xMiddle = getWidth() / 2;
        yMiddle = getHeight() / 2;
        int xStart;
        int yStart;
        xStart = getWidth() / 8;
        yStart = getHeight() / 2;
        System.out.println(structure.getBaseSequence().length);
        drawLinearFeynmann(g2d);
        //g2d.drawString("a", xStart, yStart);
        //g2d.drawString(Character.toString(structure.getSequence()[0]), xStart, yStart);
    }

    protected void drawLinearFeynmann(Graphics2D g2d) {
        char[] baseSequence = structure.getBaseSequence();
        for (int i = 0; i < baseSequence.length; i++) {
            int x = 10 * i;
            drawElement(baseSequence[i], x, 250, g2d);
        }
        LinkedList<Knot> knots = structure.getKnots();

        while (!knots.isEmpty()) {
            Knot k = knots.pop();
            int b1X = k.getB1().getX();
            int b2X = k.getB2().getX();
            drawArc(b1X, (b2X - b1X), 100, g2d);
        }
    }

    /**
     * 
     * @param x
     * @param width
     * @param height
     * @param g2 
     */
    private void drawArc(int x, int width, int height, Graphics2D g2d) {
        int y = 180;
        g2d.draw(new Arc2D.Double(x, y, width, height, 0, 180, Arc2D.OPEN));
    }

    private void drawElement(char element, int x, int y, Graphics2D g2d) {
        String str = Character.toString(element);
        g2d.drawString(str, x, y);
    }

    /**
     * @param structure the structure to set
     */
    public void setStructure(Structure structure) {
        this.structure = structure;
    }
}
